package com.syxp.pda.action.system;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.syxp.pda.core.web.action.BaseAction;
import com.syxp.pda.entity.base.Duty;
import com.syxp.pda.entity.base.User;
import com.syxp.pda.entity.system.Role;
import com.syxp.pda.service.base.DepartmentService;
import com.syxp.pda.service.base.UserService;

public class UserRoleAction extends BaseAction {
	@Resource
	private UserService userService;
	@Resource
	private DepartmentService departmentService;
	
	private int departmentID;
	private int userID;
	private int roleID;
	private int oldUserID;
	private int oldRoleID;
	
	public String load() {
		try{
			List<User> all = this.departmentService.getUserWithRole(this.departmentID) ;
			JsonArray jarray = new JsonArray() ;
			for(User user : all) {
				JsonObject jobj = new JsonObject() ;
				jobj.addProperty("userID", user.getUserID()) ;
				jobj.addProperty("operatorCode", user.getOperatorCode()) ;
				jobj.addProperty("userName", user.getUserName()) ;
				jobj.addProperty("userAccount", user.getUserAccount()) ;
				jobj.addProperty("roleID", ((Role)user.getRoles().toArray()[0]).getRoleID()) ;
				jobj.addProperty("roleName", ((Role)user.getRoles().toArray()[0]).getRoleName()) ;
				jarray.add(jobj) ;
			}
			this.jsonString = gson.toJson(jarray) ;
			System.out.println(jsonString) ;
			return SUCCESS ;
		}catch(Exception e){
			e.printStackTrace();
			return SUCCESS ;
		}
	}
	

	
	public String add() {
		try {
			this.userService.setRole(userID, roleID);
			this.jsonString = "{success:true}";
		} catch(Exception e) {
			this.jsonString = "{success:false}";
		}
		return SUCCESS;
	}
	
	public String update() {
		try {
			this.userService.modifyRole(oldUserID, oldRoleID, userID, roleID);
			this.jsonString = "{success:true}";
		} catch(Exception e) {
			this.jsonString = "{success:false}";
		}
		return SUCCESS;
	}
	
	public String delete() {
		try {
			this.userService.removeRole(userID, roleID);
			this.jsonString = "{success:true}";
		} catch(Exception e) {
			this.jsonString = "{success:false}";
		}
		return SUCCESS;
	}
	
	public String getUsers() {
		List users = new ArrayList();
		try {
			Set<User> set = this.departmentService.getUsers(departmentID);
			for(User u: set) {
				Map map = new HashMap();
				map.put("userID", u.getUserID());
				map.put("userName", u.getUserName());
				map.put("userOptName", u.getOperatorCode()+"-"+u.getUserName());
				users.add(map);
				map = null;
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
		this.jsonString = gson.toJson(users);
		return SUCCESS;
	}
	
	/**
	 * @return the userService
	 */
	public UserService getUserService() {
		return userService;
	}
	/**
	 * @param userService the userService to set
	 */
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	/**
	 * @return the departmentService
	 */
	public DepartmentService getDepartmentService() {
		return departmentService;
	}
	/**
	 * @param departmentService the departmentService to set
	 */
	public void setDepartmentService(DepartmentService departmentService) {
		this.departmentService = departmentService;
	}
	/**
	 * @return the departID
	 */
	public int getDepartmentID() {
		return departmentID;
	}
	/**
	 * @param departID the departID to set
	 */
	public void setDepartmentID(int departmentID) {
		this.departmentID = departmentID;
	}
	/**
	 * @return the userID
	 */
	public int getUserID() {
		return userID;
	}
	/**
	 * @param userID the userID to set
	 */
	public void setUserID(int userID) {
		this.userID = userID;
	}
	/**
	 * @return the roleID
	 */
	public int getRoleID() {
		return roleID;
	}
	/**
	 * @param roleID the roleID to set
	 */
	public void setRoleID(int roleID) {
		this.roleID = roleID;
	}



	/**
	 * @return the oldRoleID
	 */
	public int getOldRoleID() {
		return oldRoleID;
	}



	/**
	 * @param oldRoleID the oldRoleID to set
	 */
	public void setOldRoleID(int oldRoleID) {
		this.oldRoleID = oldRoleID;
	}



	/**
	 * @return the oldUserID
	 */
	public int getOldUserID() {
		return oldUserID;
	}



	/**
	 * @param oldUserID the oldUserID to set
	 */
	public void setOldUserID(int oldUserID) {
		this.oldUserID = oldUserID;
	}
	
}
